package cn.edu.nchu.fragmentdemo.myComponents.components;

import android.content.Context;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.edu.nchu.fragmentdemo.MainActivity;
import cn.edu.nchu.fragmentdemo.utils.DFM;

import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.Marker;
import org.osmdroid.views.overlay.infowindow.InfoWindow;

import java.io.Serializable;
import java.sql.SQLException;


public class MyMarkerClass extends Marker implements Serializable {
    public String TAG = "MyMarkerClass";
    private MapView mMapView;
    private MainActivity mainActivity;
    private InfoWindow infoWindow;
    private TextView textView;
    private LinearLayout linearLayout;

    public MyMarkerClass(MapView mapView) {
        super(mapView);
        this.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_CENTER);
        mMapView = mapView;
        mainActivity = (MainActivity) MainActivity.getActivity();
        linearLayout = new LinearLayout(mainActivity);
        infoWindow = new InfoWindow(linearLayout,mMapView) {
            @Override
            public void onOpen(Object item) {}
            @Override
            public void onClose() {}
        };
        textView =  new TextView(mainActivity);
    }

    public MyMarkerClass(MapView mapView, Context resourceProxy) {
        super(mapView, resourceProxy);
    }

    public String IPtitle;
    public String CPtitle;
    public String EPtitle;

    //暂时顶替高度
    public int Hight = 0;
    public String name = "";

    public GeoPoint centerPosition;

    public GeoPoint getCenterPosition() {
        return centerPosition;
    }

    public void setCenterPosition(GeoPoint centerPosition) {
        this.centerPosition = centerPosition;
    }

    public GeoPoint getThisGeo() {
        return new GeoPoint(this.getPosition().getLatitude(), this.getPosition().getLongitude());
    }


//    /**
//     * lqj:20200403
//     * 对JTAC设置一个infowindow，显示经纬单
//     */
//
//    public void setCustomIW(){
//        textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
//                LinearLayout.LayoutParams.WRAP_CONTENT));
//        textView.setBackgroundColor(Color.parseColor("#800000ff"));
//        textView.setText(DFM.printLongLongDFM(this.getPosition().getLongitude())+"\n"
//                +DFM.printLatLongDFM(this.getPosition().getLatitude()));
//        textView.setTextSize(8);
//        textView.setTextColor(Color.YELLOW);
//        textView.setPadding(5, 5, 5, 5);
//        linearLayout.addView(textView);
//        infoWindow.setRelatedObject(this);
//        infoWindow.open(this, this.getPosition(),0,-40);
//    }

    public void goToGeo(GeoPoint geoPoint,float rotation){
        this.setPosition(geoPoint);
        this.setRotation(rotation);
        textView.setText(DFM.printLongLongDFM(this.getPosition().getLongitude())+"\n"
                +DFM.printLatLongDFM(this.getPosition().getLatitude()));
        infoWindow.open(this, this.getPosition(),0,-40);
        Log.i(TAG,rotation+":"+rotation);
    }

    public void deleteInfoWindow(){
        infoWindow.close();
    }

    //新加的：callback
    private DataChangeListener dataChangeListener;// 消息处理完成的监听器

    /**
     * 数据获取完成的监听器
     */
    public interface DataChangeListener {
        void setData(Object result) throws SQLException;
    }

    //
    public void sendMessage(Object message) throws SQLException {
        if (dataChangeListener != null) {
            dataChangeListener.setData(message);// 设置监听器，进行后续操作
        }
    }

    public DataChangeListener getDataChangeListener() {
        return dataChangeListener;
    }

    public void setDataChangeListener(DataChangeListener dataChangeListener) {
        this.dataChangeListener = dataChangeListener;
    }
}
